//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.panels;


import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;
import com.openbravo.pos.base.AppLocal;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class JObjectListFinder<T> extends AbstractKeyBoardJDialog {
    
    private Object m_sDialogTicket;
    private AbstractListModel model;
    private List<T> tickets;
    private int selectIndex;

    /** Creates new form JUserList 
     * @wbp.parser.constructor*/
    private JObjectListFinder(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /** Creates new form JUserList */
    private JObjectListFinder(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public Object showUserList(java.util.List<T> atickets) {
        m_sDialogTicket = null;
        if(atickets==null||atickets.size()==0)
            return m_sDialogTicket;
        tickets=atickets;
        model=new AbstractListModel(){
            @Override
            public int getSize() {
                return tickets.size();
            }

            @Override
            public Object getElementAt(int index) {
                return tickets.get(index);
            }
        };
        selectIndex=0;
        jList1.setModel(model);
        jList1.setSelectedIndex(selectIndex);
        setVisible(true);
        return m_sDialogTicket;
    }
        
    public static JObjectListFinder newJDialog(Component ticketsbagshared) {
        
        Window window = getWindow(ticketsbagshared);
        JObjectListFinder mydialog;
        if (window instanceof Frame) { 
            mydialog = new JObjectListFinder((Frame) window, true);
        } else {
            mydialog = new JObjectListFinder((Dialog) window, true);
        } 
        
        
        mydialog.jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
        
        return mydialog;
    }
    
      @Override
    protected void enterKeyProc() {
        m_sDialogTicket = tickets.get(jList1.getSelectedIndex());
        dispose();
    }

    @Override
    protected void escapeKeyProc() {
        dispose();
    }

    @Override
    protected void downKeyProc() {
        if(tickets.size()<=0)
            return;
        selectIndex=jList1.getSelectedIndex();
        if(selectIndex<tickets.size()-1){
            selectIndex++;
        }else{
            selectIndex=0;
        }
         jList1.setSelectedIndex(selectIndex);
    }

    @Override
    protected void upKeyProc() {
        if(tickets.size()<=0)
            return;
        selectIndex=jList1.getSelectedIndex();
        if(selectIndex>0){
            selectIndex--;
        }else{
            selectIndex=tickets.size()-1;
        }
         jList1.setSelectedIndex(selectIndex);
    }

    class MyCellRenderer extends JLabel implements ListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
         String s = value.toString();
         setText(s);
         setFont(new java.awt.Font ("Dialog", 1, 24));
         if (isSelected) {
             setBackground(list.getSelectionBackground());
             setForeground(list.getSelectionForeground());
         } else {
             setBackground(list.getBackground());
             setForeground(list.getForeground());
         }
         setEnabled(list.isEnabled());
         setOpaque(true);
         return this;
        }
 }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jcmdOK = new javax.swing.JButton();
        m_jButtonCancel = new javax.swing.JButton();

        setTitle(AppLocal.getIntString("caption.tickets")); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setViewportView(jList1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jcmdOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        jcmdOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        jcmdOK.setFocusPainted(false);
        jcmdOK.setFocusable(false);
        jcmdOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        jcmdOK.setRequestFocusEnabled(false);
        jcmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmdOKActionPerformed(evt);
            }
        });
        jPanel3.add(jcmdOK);

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Close")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });
        jPanel3.add(m_jButtonCancel);

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-340)/2, (screenSize.height-272)/2, 340, 272);
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed

        dispose();
        
    }//GEN-LAST:event_m_jButtonCancelActionPerformed

    private void jcmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdOKActionPerformed
        m_sDialogTicket = tickets.get(jList1.getSelectedIndex());
        dispose();
    }//GEN-LAST:event_jcmdOKActionPerformed
       
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jcmdOK;
    private javax.swing.JButton m_jButtonCancel;
    // End of variables declaration//GEN-END:variables
    
}
